﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.BizTalk.Bam.WebServices.ManagementService;
using Microsoft.BizTalk.Bam.Portal;

public partial class Pages_ViewPageInner : System.Web.UI.Page
{
    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set the content type as XML
        Response.ContentType = "text/xml";
        // Write the XML header
        Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        // Get the parameters
        string id = this.Context.Request.QueryString["id"];
        string name = this.Context.Request.QueryString["name"];
        Reference[] refs = null;
        try
        {
            // Try to get a reference with the given id
            // This will work only for the initial message body but all other will fail since they are actually added with the contiuated activity Id and not the real one.
            refs = BAMWebServicesFactory.ManagementWebServiceProxy.GetReferences("LIU ESB Progress", "LIUESBMsg", id, "DocumentUrl");
        }
        catch (System.Web.Services.Protocols.SoapException ex)
        {
            // It failed so this means we are tracking this item using the business Id (Message Id) and not the initial activity Id (itinerary uuid)
            // Search for an activity that has the Message Id is equal to the given activity Id
            Microsoft.BizTalk.Bam.WebServices.QueryService.InstanceQuery query = new Microsoft.BizTalk.Bam.WebServices.QueryService.InstanceQuery();
            query.SelectClauses = new string[0];
            query.WhereClauses = new Microsoft.BizTalk.Bam.WebServices.QueryService.ArrayOfWhereClausesFilterFilter[1];
            query.WhereClauses[0] = new Microsoft.BizTalk.Bam.WebServices.QueryService.ArrayOfWhereClausesFilterFilter();
            query.WhereClauses[0].Field = "MessageId";
            query.WhereClauses[0].Condition = "CONTAINS";
            query.WhereClauses[0].Logic = "And";
            query.WhereClauses[0].Value = id;
            Microsoft.BizTalk.Bam.WebServices.QueryService.Column[][] results = BAMWebServicesFactory.QueryWebServiceProxy.GetInstanceData("LIU ESB Progress", "LIUESBMsg", query, 120);
            if (results.Length > 0)
            {
                if (results[0].Length > 0 && results[0][0].Name == "ActivityID")
                {
                    id = results[0][0].Value;
                    // Get all the references for the found activity Id
                    refs = BAMWebServicesFactory.ManagementWebServiceProxy.GetReferences("LIU ESB Progress", "LIUESBMsg", id, "DocumentUrl");
                }
            }
        }
        if (refs!=null && refs.Length > 0)
        {
            foreach (Reference Ref in refs)
            {
                if (Ref.Name == name)
                {
                    // Search for the one reference that has the same name as the requested refernce
                    // This relies on the fact that reference names should be unique within the same activity instance
                    // Wite this complete XML
                    Response.Write(Ref.LongReferenceData);
                    break;
                }
            }
        }
        // End the response stream
        Response.End();
    }
}